import pinia from '@/store/index';
import {
	useloginStore
}
from '@/store/login';
const loginStore = useloginStore(pinia);

export async function saveInfo(type) {

	const userInfo = JSON.parse(JSON.stringify(loginStore.userInfo));
	const params = Object.assign(userInfo, type);

	try {
		const res = await this.$api.msgApi.updateUserInfo(params);
		if (res.code === 0) {
			uni.showToast({
				title: `${res.msg || '保存成功'}`,
				icon: 'none'
			});

			const data = JSON.parse(JSON.stringify(res.data));
			loginStore.login(data);
			uni.setStorageSync('userInfo', data);

			return {
				success: true,
				data
			};
		}
		else {
			uni.showToast({
				title: `${res.msg || '保存失败'}`,
				icon: 'none'
			});

			return {
				success: false,
				error: res.msg
			};
		}
	}
	catch (error) {
		uni.showToast({
			title: '请求失败',
			icon: 'none'
		});

		return {
			success: false,
			error
		};
	}
}